package user.zc.action;

import user.zc.action.comp.ManagerAction;
import user.zc.config.value.Key;
import user.zc.controller.ISystemTemplate;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.TimerTask;
import java.util.stream.Collectors;

public class ManagerSystemTemplateAction extends ManagerAction<Key> {
    private ISystemTemplate templateController;
    public ManagerSystemTemplateAction(ISystemTemplate templateController) {
        super(new String[]{"名称"}, (dto)->{
            try {
                templateController.delete(dto.getKey());
            } catch (Exception e) {
                e.printStackTrace();
            }
        },  (dto,callback)->{
            try {
                new AddOrUpdateSystemTemplateAction(templateController,dto.getKey(),templateController.read(dto.getKey()),(value)->{
                    //成功的画，需要重新刷新
                    callback.accept(true);
                });
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null,
                        e.getMessage(), "错误提示", JOptionPane.ERROR_MESSAGE);
            }
        });
        this.templateController = templateController;
        setTitle("连接管理");
        refresh();
    }

    @Override
    protected List<Key> refreshRow(DefaultTableModel tableModel) throws Exception{
        List<String> list = templateController.list();
        list.forEach(dto->{
            tableModel.addRow(new Object[]{dto});
        });
        return list.stream().map(t->new Key(t)).collect(Collectors.toList());
    }
}
